package cn.iocoder.yudao.module.book.service.user;

import cn.iocoder.yudao.module.book.controller.app.user.vo.UserReadingStatsRespVO;
import cn.iocoder.yudao.module.book.dal.mysql.records.RecordsMapper;
import cn.iocoder.yudao.module.book.dal.mysql.users.userDTOMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
public class UserStatsServiceImpl implements UserStatsService {

    @Resource
    private RecordsMapper recordsMapper;
    
    @Resource
    private userDTOMapper userMapper;

    @Override
    public UserReadingStatsRespVO getUserReadingStats(Long userId) {
        UserReadingStatsRespVO result = new UserReadingStatsRespVO();

        // 今日阅读数量
        String today = LocalDate.now().toString();
        result.setTodayRead(recordsMapper.countTodayRead(userId, today));

        // 本周阅读数量
        String weekStart = LocalDate.now().minusDays(6).toString();
        result.setWeekRead(recordsMapper.countWeekRead(userId, weekStart));

        // 阅读天数
        result.setReadDays(recordsMapper.countReadDays(userId));

        // 加入天数
        LocalDateTime userCreateTime = userMapper.selectById(userId).getCreateTime();
        long joinDays = ChronoUnit.DAYS.between(userCreateTime.toLocalDate(), LocalDate.now());
        result.setJoinDays((int) joinDays);

        // 本月阅读数量
        String monthStart = LocalDate.now().withDayOfMonth(1).toString();
        result.setMonthRead(recordsMapper.countMonthRead(userId, monthStart));

// 阅读总数量
        result.setTotalRead(recordsMapper.countTotalRead(userId));

        // 最近阅读记录
        result.setList(recordsMapper.selectRecentReadingWithBooks(userId, 10));

        return result;
    }
    @Override
    public List<UserReadingStatsRespVO.ReadingRecordItem> getTodayReadList(Long userId) {
        String today = LocalDate.now().toString();
        return recordsMapper.selectTodayReadingWithBooks(userId, today);
    }
}